/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.kramerius.rukopisy.tools.cli;

import cz.mzk.kramerius.commons.Model;
import cz.mzk.kramerius.commons.PidList;
import cz.mzk.kramerius.fedoraaccess.FedoraConnector;
import cz.mzk.kramerius.fedoraaccess.FedoraServer;
import cz.mzk.kramerius.fedoraaccess.metadataUpdate.object.ModelChanger;
import cz.mzk.kramerius.fedoraaccess.metadataUpdate.object.ObjectUpdater;
import cz.mzk.kramerius.fedoraaccess.rest.RESTConnector;
import java.io.File;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Martin Řehánek <rehan at mzk.cz>
 */
public class UpdateModelsAction implements Action {

    private static final Logger logger = Logger.getLogger(UpdateModelsAction.class.getName());
    private final File pidList;
    private final File properties;
    private final String model;

    public UpdateModelsAction(File properties, File pidList, String model) {
        this.pidList = pidList;
        this.properties = properties;
        this.model = model;
    }

    public void run() throws Exception {
        FedoraConnector con = new RESTConnector(new FedoraServer(properties));
        List<String> pids = (new PidList(pidList)).content();
        updateModels(pids, con);
    }

    private void updateModels(List<String> pids, FedoraConnector con) {
        ObjectUpdater updater = new ModelChanger(con, new Model(model));
        for (String pid : pids) {
            try {
                updater.updateObject(pid);
            } catch (Throwable e) {
                logger.log(Level.SEVERE, "failed to update foxml", e);
            }
        }
    }
}
